package com.detrum.costomview;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.detrum.base.R;


/**
 * Created by abc on 2017/7/24.
 */

public class BaseTitle extends RelativeLayout implements View.OnClickListener{
    private Context context;
    private Boolean ok_b,more_b;
    private String title,ok;
    private TextView titleName,titleOk;
    private ImageView titleBack,titleMore;
    private BaseTitleCall baseTitleCall;
    private RelativeLayout back_rl;
    private boolean back;
    private int titlemore,titleback;
    public BaseTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        LayoutInflater.from(context).inflate(R.layout.base_title,this,true);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.BaseTitle);
        title=typedArray.getString(R.styleable.BaseTitle_titleName);
        ok=typedArray.getString(R.styleable.BaseTitle_titleOk);
        ok_b=typedArray.getBoolean(R.styleable.BaseTitle_titleOk_b,true);
        more_b=typedArray.getBoolean(R.styleable.BaseTitle_titleMore_b,false);
        titlemore=typedArray.getResourceId(R.styleable.BaseTitle_titleMore,0);
        back=typedArray.getBoolean(R.styleable.BaseTitle_titleBack,true);
        titleback = typedArray.getResourceId(R.styleable.BaseTitle_nameBack,0);
        typedArray.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        back_rl= findViewById(R.id.title_layout);
        titleName= findViewById(R.id.title_txt);
        titleOk= findViewById(R.id.title_right_txt);
        titleMore= findViewById(R.id.my_title_iv);
        titleBack= findViewById(R.id.title_back_icon);
        if(!TextUtils.isEmpty(title)){
            titleName.setText(title);
        }
        if(!TextUtils.isEmpty(ok)){
            titleOk.setText(ok);
        }
        if(ok_b){
            titleOk.setVisibility(View.VISIBLE);
        }else{
            titleOk.setVisibility(View.GONE);
        }
        if(more_b){
            titleMore.setVisibility(View.VISIBLE);
            titleOk.setVisibility(View.GONE);
            if(titlemore!=0){
                titleMore.setImageResource(titlemore);
            }
        }
        if(!back){
            titleBack.setVisibility(View.GONE);
        }
        if(titleback!=0){
            titleName.setBackgroundResource(titleback);
        }
        titleOk.setOnClickListener(this);
        titleMore.setOnClickListener(this);
        titleBack.setOnClickListener(this);
    }

    public void setBaseTitleCall(BaseTitleCall baseTitleCall){
        this.baseTitleCall=baseTitleCall;
    }

    public void setOk(String ok){
        titleOk.setText(ok);
    }

    public String getOk(){
        return titleOk.getText().toString().trim();
    }

    public void setTitle(String title){
        titleName.setText(title);
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.title_right_txt) {
            baseTitleCall.Click(view, BaseTitleCall.OK);

        } else if (i == R.id.my_title_iv) {
            baseTitleCall.Click(view, BaseTitleCall.MORE);

        } else if (i == R.id.title_back_icon) {
            ((Activity) context).finish();

        }
    }
}
